package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.MyMessage;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.database.*;
import com.wwwc.util.web.MyHtml;

public class IndexBean {
    private EJBDatabaseQuery ejbdb;

    public boolean blockIp(String user_ip, String block_by, String policy, String username, String target_page, String duration) {
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    String query = "INSERT INTO BLOCKED_IPS(FIELD1, TIMESTAMP, USER_IP, TIME,"+
		" BLOCKED_BY, POLICY, USERNAME, TARGET_PAGE, FIELD9)"+
		"VALUES(null, 'now','"+user_ip+"','"+System.currentTimeMillis()+
		"','"+block_by+"','"+policy+"','"+username+"','"+target_page+"', '"+duration+"');" ;
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    ejbdb.insert(query);
	}
	catch (Exception e) {
	}
	return true;
    }

    public String getPageColor(String page_id) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    
	    String query = "SELECT * FROM PAGE_COLOR WHERE PAGE_ID="+page_id+";";
	    ArrayList a = ejbdb.selectSingleRecord(query);

	    if(a == null || a.size()==0) {
		query = "SELECT * FROM PAGE_COLOR WHERE PAGE_ID=5;";
		a = ejbdb.selectSingleRecord(query);
	    }

	    String pc_id   = null;
	    String bgcolor = null;
	    String text    = null;
	    String link    = null;
	    String vlink   = null;
	    String alink   = null;

	    page_id = (String)a.get(2);
	    bgcolor = (String)a.get(3);
	    text    = (String)a.get(4);
	    link    = (String)a.get(5);
	    vlink   = (String)a.get(6);
	    alink   = (String)a.get(7);
	  
	    sbf.append("<BODY bgcolor='"+bgcolor+"' text='"+text+"' link='"+link+"' vlink='"+vlink+"' ");
	    sbf.append(" alink='"+alink+"'");
	    //sbf.append(" alink='"+alink+"' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' ");
	    //sbf.append(" onMouseMove='mouseMove()'");
	    //sbf.append(" onMouseUp='mouseMove()'");
	    sbf.append(">");
	}
	catch (Exception e) {
	}
	return sbf.toString();
    }
    /*
    public StringBuffer setPageColor(String call_path, String mid, String cpc_id, boolean manager, boolean admin) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String query = "SELECT * FROM PAGE_COLOR ORDER BY PAGE_ID;";
	    ArrayList alist = ejbdb.select(query);
	    Iterator it = alist.iterator();
	    ArrayList a = null;

	    String page_id = null;
	    String bgcolor = null;
	    String text    = null;
	    String link    = null;
	    String vlink   = null;
	    String alink   = null;

	    sbf.append("<BR><BR>");
	    sbf.append("<FORM name='PageColor' method='post' action='"+call_path+"'>");
	    sbf.append("<TABLE border='1'>");
	    sbf.append("<TR>");
	    sbf.append("<TD>&nbsp;</TD>");
	    sbf.append("<TD><B>Background Color</B></TD>");
	    sbf.append("<TD><B>Text Color</B></TD>");
	    sbf.append("<TD><B>Unvisited Link Color</B></TD>");
	    sbf.append("<TD><B>Visited Link Color</B></TD>");
	    sbf.append("<TD><B>Alive Link Color</B></TD>");
	    sbf.append("</TR>");

	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		page_id = (String)a.get(2);
		bgcolor = (String)a.get(3);
		text    = (String)a.get(4);
		link    = (String)a.get(5);
		vlink   = (String)a.get(6);
		alink   = (String)a.get(7);

		sbf.append("<TR>");
		if(cpc_id != null && cpc_id.equals(page_id)) {
		    sbf.append("<TD><input type='radio' name='pc_id' value='"+page_id+"' checked></TD>");
		}
		else {
		    sbf.append("<TD><input type='radio' name='pc_id' value='"+page_id+"'></TD>");
		}
		sbf.append("<TD bgcolor="+bgcolor+">&nbsp;</TD>");
		sbf.append("<TD bgcolor="+text+">&nbsp;</TD>");
		sbf.append("<TD bgcolor="+link+">&nbsp</TD>");
		sbf.append("<TD bgcolor="+vlink+">&nbsp</TD>");
		sbf.append("<TD bgcolor="+alink+">&nbsp;</TD>");
		sbf.append("</TR>");		
	    }

	    sbf.append("</TABLE>");
	    sbf.append("<input type='hidden' name='mid' value='"+mid+"'>");
	    sbf.append("</FORM>");

	    sbf.append("<BR><BR>");
	    sbf.append("<TABLE border='0'>");
	    sbf.append("<TR>");
	    sbf.append(MyHtml.makeLinkCell("Change","javaScript:document.PageColor.submit();"));
	    if(manager || admin) {
		sbf.append(MyHtml.makeLinkCell("Edit", call_path+"?mid="+mid+"&aid=4-1"));
	    }
	    sbf.append("</TR>");
	    sbf.append("</TABLE>");
	}
	catch (Exception e) {
	}

	return sbf;
    }
    */

    public static StringBuffer getScreenInfo (String call_path) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<html>");
	sbf.append("<body>");
	sbf.append("<form name='screen_form' method='post' action='"+call_path+"'>");
	sbf.append("<input type='hidden' name='screen_check' value='true'>");
	sbf.append("<input type='hidden' name='sw' value='1280'>");
	sbf.append("<input type='hidden' name='sh' value='1024'>");
	sbf.append("</form>\n"); 
	sbf.append("<SCRIPT>");
	sbf.append("document.screen_form.sw.value=screen.width;");
	sbf.append("document.screen_form.sh.value=screen.height;");
	sbf.append("document.screen_form.submit();");
	sbf.append("</SCRIPT>");
	sbf.append("</body>\n"); 
	sbf.append("</html>\n"); 
	return sbf;
    }
    
    public StringBuffer getMovingImageScript() {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<SCRIPT>"); 
	sbf.append("var xPos = 20;");
	sbf.append("var yPos = document.body.clientHeight;");
	sbf.append("var step = 1;");
	sbf.append("var delay = 30;");
	sbf.append("var height = 0;");
	sbf.append("var Hoffset = 0;");
	sbf.append("var Woffset = 0;");
	sbf.append("var yon = 0;");
	sbf.append("var xon = 0;");
	sbf.append("var pause = true;");
	sbf.append("var interval;");
	sbf.append("img.style.top = yPos;");

	sbf.append("function changePos() {");
	sbf.append("width  = document.body.clientWidth;");
	sbf.append("    height = document.body.clientHeight;");
	sbf.append("    Hoffset = img.offsetHeight;");
	sbf.append("    Woffset = img.offsetWidth;");
	sbf.append("    img.style.left = xPos + document.body.scrollLeft;");
	sbf.append("    img.style.top  = yPos + document.body.scrollTop;");
	sbf.append("    if (yon) {");
	sbf.append("        yPos = yPos + step;");
	sbf.append("    }");
	sbf.append("    else {");
	sbf.append("        yPos = yPos - step;");
	sbf.append("    }");
	sbf.append("    if (yPos < 0) {");
	sbf.append("        yon = 1;");
	sbf.append("        yPos = 0;");
	sbf.append("    }");
	sbf.append("    if (yPos >= (height - Hoffset)) {");
	sbf.append("        yon = 0;");
	sbf.append("        yPos = (height - Hoffset);");
	sbf.append("    }");
	sbf.append("    if (xon) {");
	sbf.append("        xPos = xPos + step;");
	sbf.append("    }");
	sbf.append("    else {");
	sbf.append("       xPos = xPos - step;");
	sbf.append("    }");
	sbf.append("    if (xPos < 0) {");
	sbf.append("        xon = 1;");
	sbf.append("        xPos = 0;");
	sbf.append("    }");
	sbf.append("    if (xPos >= (width - Woffset)) {");
	sbf.append("       xon = 0; xPos = (width - Woffset);");
	sbf.append("    }");
	sbf.append("}");

	sbf.append("function www_helpor_net() {");
	sbf.append("    img.visibility = \"visible\";");
	sbf.append("    interval = setInterval('changePos()', delay);");
	sbf.append("}");

	sbf.append("www_helpor_net();");
	sbf.append("</SCRIPT>");
	return sbf;
    }
}






